<?php
namespace Cyfy\Modules\BasicContent\Forms;

use \Cyfy\Message;
use \Cyfy\Modules\Forms;
use \Cyfy\Modules\BasicContent\Content;
use \Cyfy\Modules\Menu\Menu;


/**
 * Edits a page
 */
class EditPage extends Forms\Form
{
    public $bcData = array();
    
	function structure()
	{
        // other items
        $i = new Forms\TextBox("path", self :: t(30));
		$i -> required = true;
		$i -> maxLength = 255;
		$this -> addItem($i);
		
		$i = new Forms\Select("menu", self :: t(270));
		$i -> multiple = true;
		$this -> addItem($i);
        
    
        // LANGUAGES
        $t = new Forms\TabSet("texts", "Texts");
        $t -> defaultTab = ($_GET['language']) ? $_GET['language'] : \Cyfy\Language :: getActive();
    
        foreach(\Cyfy\Language :: getAll() as $lg => $language)
        {
            $bc = (array) $this -> bcData[$lg];
            
            $t -> addTab($lg, $language);
            $i = new Forms\TextBox("title[$lg]", self :: t(20));
            $i -> maxlength = 255;
            if($bc){
                $i -> value = (string) $bc['title'];
            }
            
            $t -> addItem($lg, $i);
            
            $w = new Forms\WysiwygEditor("content[$lg]", self :: t(40));
            if($bc){
                $w -> value = (string) $bc['content'];
            }
            
            $t -> addItem($lg, $w);
        }
        
        $this -> addItem($t);
	
        
        
		$i = new Forms\Hidden("oldPath");
		$this -> addItem($i);
		
		$i = new Forms\Hidden("bcid");
		$this -> addItem($i);
		
		$b = new Forms\Button(self :: t(280), Forms\Button::OK);
		$this -> addButton($b);
		
		$b = new Forms\Button(self :: t(290), Forms\Button::RESET);
		$b -> onClick = "if(confirm('" . self :: t(300) . "')){ Cyfy.refreshPage(); } return false;";
		$this -> addButton($b);
		
        $this -> permission('BasicContent', 'edit own');
		$this -> stay = true;
		$this -> wysiwygForm = true;
    }
  
	function validate()
	{
		// oldpath == newpath?
		if($this -> get("path") == $this->get("oldPath"))
		{
			return true;
		}

        // path OK?
        if(\Cyfy\Cyfy ::  tryPath($this -> get("path")))
        {
            return true;
        }

        // path used
        Message :: set(self :: t(231, array($this -> get("path"))), Message::ERROR);
        return false;
	}
  
	function submit()
	{  
		$bcid = $this -> get("bcid");
        
        // a little hack while using POST
        $titles = $_POST['title'];
        $contents =  $_POST['content'];
        
         foreach(\Cyfy\Language :: getAll() as $lg => $language)
        {
            if($contents[$lg])
            {
                $title = ($titles[$lg]) ? $titles[$lg] : "";
                $content = $contents[$lg];
                
                // content
                $c = new Content($title, $content, $lg);
                $c -> setBCID($bcid);
                
                if(!$c -> save())
                {
                    Message :: set(self :: t(341, array($language)), Message::ERROR);
                    continue;
                }
            }
        }
        
        // oldpath != newpath?
		if($this -> get("path") != $this->get("oldPath"))
        {
            // update FS
            //$path = strtr($this -> get("path"), array("/" => "\/"));
            //$oldPath = strtr($this -> get("oldPath"), array("/" => "\/"));
            \Cyfy\Cyfy ::  unregisterPath($this -> get("oldPath"));
            \Cyfy\Cyfy ::  registerPath($this -> get("path"), "BasicContent", $bcid, '');
            
            // update links in menus
            Menu :: updateLinks($this -> get("oldPath"), $this -> get("path"));
            $this -> action = "administration/BasicContent/pagesList/" . $bcid;
        }
         // menus
        $newMenus = $this -> get("menu");
        
        // load old menus
        $oldMenus = array();
        $result = \dibi :: query("SELECT mid FROM [:cyfy:Menu-items] WHERE [link] = %s", $this -> get("path"), " AND [language] = %s", \Cyfy\Language :: getActive());
        foreach($result as $m)
        {
            $oldMenus[] = $m['mid'];
        }       
        
        // old menus
        if(is_array($oldMenus))
        {
            $addNew = false;
        }
        else
        {
            $addNew = true;
        }
        
        if(is_array($newMenus)){
            // going thru new menu items
            foreach($newMenus as $mid)
            {
                $addThis = $addNew;
                // if wasn't in the old
                if(!$addThis)
                {
                    if(!$oldMenus || !in_array($mid, $oldMenus))
                    {
                        $addThis = true;
                    }
                }
                // add
                if($addThis)
                {
                    $lg = \Cyfy\Language :: getActive();
                    if($titles[$lg])
                    {
                        Menu :: addItem($titles[$lg], $this->get("path"), $mid, $lg);
                    }
                }
            }
        }
        
        if(!$addNew){
            // going thru old menu items
            foreach($oldMenus as $mid)
            {
                // if the new menu list is empty
                if(!is_array($newMenus))
                {
                    Menu :: removeItemByLink($this -> get("path"), $mid);
                }
                else
                {
                    // if the old is not in the new - delete
                    if(!in_array($mid, $newMenus))
                    {
                        Menu :: removeItemByLink($this -> get("path"), $mid);
                    }
                }
            }
        }
    
        // message
        Message :: set(self :: t(351, array($this -> get("title"))));
        Message :: set(self :: t(360), Message::WARNING);
		return true;
	}	
}


